package net.sudot.chess.business.wechat.message;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import net.sudot.chess.business.model.Customer;
import net.sudot.chess.business.model.OrderGame;
import net.sudot.chess.business.model.User;
import net.sudot.chess.config.SystemProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 退款通知
 * <pre>
 * {{first.DATA}}
 *
 * 退款原因：{{reason.DATA}}
 * 退款金额：{{refund.DATA}}
 * {{remark.DATA}}
 * <p/>
 * 最终发送示例:
 * <p/>
 * 您已退出xxx
 *
 * 退款原因：退出游戏
 * 退款金额：20金豆
 * 账户余额：120金豆
 *
 * 点击查看游戏信息
 * </pre>
 *
 * @author tangjialin on 2018-04-03.
 */
@Component
public class RefundTemplateMessage extends AbstractTemplateMessage {

    @Value("${system.config.wechat.refundTempleteId}")
    private String templateId;

    @Override
    public String getTemplateId() {
        return templateId;
    }

    @Override
    protected WxMpTemplateMessage.WxMpTemplateMessageBuilder builderTemplateMessage(User user, Object data, SystemProperties systemProperties) {
        Customer customer = (Customer) user;
        OrderGame orderGame = (OrderGame) data;

        StringBuilder remark = new StringBuilder("账户余额：").append(customer.getBalance().toPlainString()).append("金豆")
                .append("\n\n点击查看游戏信息");
        List<WxMpTemplateData> templateData = new ArrayList<>();
        templateData.add(new WxMpTemplateData("first", "您已退出" + orderGame.getMemo()));
        templateData.add(new WxMpTemplateData("reason", "退出游戏"));
        templateData.add(new WxMpTemplateData("refund", orderGame.getPaymentAmount().toPlainString() + "金豆"));
        templateData.add(new WxMpTemplateData("remark", remark.toString()));
        WxMpTemplateMessage.WxMpTemplateMessageBuilder builder = WxMpTemplateMessage.builder().data(templateData);
        builder.url(systemProperties.getWebsite() + "/game/detail/" + orderGame.getGameId());
        return builder;
    }

}
